//
//  CAISystemConfigManager.m
//  CAISystemConfigManager_Example
//
//  Created by 程杰 on 2021/11/1.
//  Copyright © 2021 cj. All rights reserved.
//

#import "CAISystemConfigManager.h"
#import "NetworkManager.h"

static NSString *const AppVersion = @"Chs_SystemDefaultConfig_AppVersion";

@interface CAISystemConfigManager ()


@property (nonatomic,assign) BOOL isFirstLaunch;
@property (nonatomic,strong) NSArray *systemDefaultArray;
@property (nonatomic,strong) NSString *currentTimeTamp;
@property (nonatomic, assign) NSInteger fileReCreateTimes;
@end

@implementation CAISystemConfigManager



////读取本地内置的一份文件
///添加单个key即时
///请求多个key其中一个没有，服务端如何返回
///模块请求不暴露

+ (instancetype)shareInstance{
    
    static CAISystemConfigManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[CAISystemConfigManager alloc] init];
        [instance initData];
    });
    return instance;
}

#pragma mark 启动SDK
-(void)launchSystemConfig:(BOOL)isLogin SuccessBlock:(SystemConfigSuccessBlock)successBlock
     failureBlock:(SystemConfigFailureBlock)failureBlock{
    if (isLogin) {
        [self getAllConfigSuccessBlock:successBlock failureBlock:failureBlock];
    }else{
        if (!self.systemDefaultArray.count) {
            return;
        }
        NSArray *array = @[@{@"moduleName":@"default",@"key":self.systemDefaultArray}];
        [self getInstantPubKeys:array SuccessBlock:successBlock failureBlock:failureBlock];
    }
}

#pragma mark 初始化SDK（读取本地文件兜底）
-(void)initData{
    self.fileReCreateTimes = 0;
    self.systemDefaultConfigMap = [NSMutableDictionary dictionary];
    self.systemDefaultArray = [NSArray array];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"appTimestamp"]) {
        self.currentTimeTamp = [[NSUserDefaults standardUserDefaults] objectForKey:@"appTimestamp"];
    }
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentAppVersion = infoDic[@"CFBundleVersion"];
    NSString *version = [[NSUserDefaults standardUserDefaults] objectForKey:@"cai_system_appVersion"];
    if (version == nil || ![version isEqualToString:currentAppVersion]) {
        [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:@"appTimestamp"];
        self.currentTimeTamp = @"";
        [[NSUserDefaults standardUserDefaults] setValue:currentAppVersion forKey:@"cai_system_appVersion"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    NSDictionary *jsonDic = [self caiReadLocalFileWithName:@"cai_app_config_default"];
    
    if (!jsonDic.allKeys.count) {
        return;
    }
    
    if ([self isFirstLauch]) {
        if ([jsonDic.allKeys containsObject:@"defaultLocalConfigMap"]) {
            NSDictionary *defaultLocalConfigMap = [jsonDic objectForKey:@"defaultLocalConfigMap"];
            self.systemDefaultConfigMap = [defaultLocalConfigMap mutableCopy];
            [self writeConfigInfo:self.systemDefaultConfigMap];
        }
    }else if (!self.systemDefaultConfigMap.allKeys.count){
//        NSDictionary *defaultLocalConfigMap = [jsonDic objectForKey:@"defaultLocalConfigMap"];
//        self.systemDefaultConfigMap = [defaultLocalConfigMap mutableCopy];
        self.systemDefaultConfigMap = [[self readConfigInfo] mutableCopy];
    }
    
    if ([jsonDic.allKeys containsObject:@"appStartKeys"]) {
        self.systemDefaultArray = [jsonDic objectForKey:@"appStartKeys"];
    }
}

#pragma mark 获取最新系统参数(根据对应key获取配置)
-(void)getInstantConfigKeys:(NSMutableArray *)keyMutableArray SuccessBlock:(SystemConfigSuccessBlock)successBlock
                    failureBlock:(SystemConfigFailureBlock)failureBlock{
    if (keyMutableArray.count > 0) {
        __weak __typeof(&*self)weakSelf = self;
        [self requestSystemConfigKeyArray:keyMutableArray SuccessBlock:^(NSArray * _Nonnull systemConfigArray) {
            if (systemConfigArray.count > 0) {
                NSDictionary *configDict = [NSDictionary dictionary];
                NSMutableDictionary *callBackDic = [NSMutableDictionary dictionary];
                NSString *configTimestamp = [[systemConfigArray objectAtIndex:0] objectForKey:@"configTimestamp"];
                if (configTimestamp) {
                    if (![self.currentTimeTamp isEqualToString:configTimestamp]) {
                        self.currentTimeTamp = configTimestamp;
                        [[NSUserDefaults standardUserDefaults] setValue:configTimestamp forKey:@"appTimestamp"];
                        [self requestSystemConfigModule:@"default"];
                    }
                }
                for (int i = 0; i<systemConfigArray.count; i++) {
                    configDict = [systemConfigArray objectAtIndex:i];
                    id configValue = [configDict objectForKey:@"configValue"];
                    NSString *configValueString;
                    if ([configValue isKindOfClass:[NSDictionary class]] || [configValue isKindOfClass:[NSArray class]]) {
                        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:configValue options:0 error:nil];
                        configValueString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                    }else{
                        configValueString = [NSString stringWithFormat:@"%@",[configDict objectForKey:@"configValue"]];
                    }
                    configValueString = [CAISystemConfigManager isEmpty:configValueString] ? @"" : configValueString;
                    [callBackDic setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
                    [weakSelf.systemDefaultConfigMap setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
                }
                [weakSelf writeConfigInfo:self.systemDefaultConfigMap];
                if (successBlock) {
                    successBlock([callBackDic copy]);
                }
            }
        } failureBlock:^(NSError * _Nonnull error) {
            if (error) {
                failureBlock(error);
            }
        }];
    }
}

#pragma mark 获取公共key(无需登录)
-(void)getInstantPubKeys:(NSArray *)keyArray SuccessBlock:(SystemConfigSuccessBlock)successBlock
                    failureBlock:(SystemConfigFailureBlock)failureBlock{
    if (keyArray.count > 0) {
        __weak __typeof(&*self)weakSelf = self;
        [self requestSystemPubKeyArray:keyArray SuccessBlock:^(NSArray * _Nonnull systemConfigArray) {
            if (systemConfigArray.count > 0) {
                NSDictionary *configDict = [NSDictionary dictionary];
                NSMutableDictionary *callBackDic = [NSMutableDictionary dictionary];
                for (int i = 0; i<systemConfigArray.count; i++) {
                    configDict = [systemConfigArray objectAtIndex:i];
                    id configValue = [configDict objectForKey:@"configValue"];
                    NSString *configValueString;
                    if ([configValue isKindOfClass:[NSDictionary class]] || [configValue isKindOfClass:[NSArray class]]) {
                        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:configValue options:0 error:nil];
                        configValueString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                    }else{
                        configValueString = [NSString stringWithFormat:@"%@",[configDict objectForKey:@"configValue"]];
                    }
                    configValueString = [CAISystemConfigManager isEmpty:configValueString] ? @"" : configValueString;
                    [callBackDic setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
                    [weakSelf.systemDefaultConfigMap setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
                }
                [weakSelf writeConfigInfo:self.systemDefaultConfigMap];
                if (successBlock) {
                    successBlock([callBackDic copy]);
                }
            }
        } failureBlock:^(NSError * _Nonnull error) {
            if (error) {
                failureBlock(error);
            }
        }];
    }
}

#pragma mark 获取全量系统参数(远端为准)
-(void)getAllConfigSuccessBlock:(SystemConfigSuccessBlock)successBlock
                   failureBlock:(SystemConfigFailureBlock)failureBlock{
    
    NSMutableDictionary *configKeyMap = [NSMutableDictionary dictionary];
    
    DataEntity * entity = [DataEntity new];
    [entity setUrlString:@"com.chs.app.mobileserver.config.timestamp"];
    [entity setParameters:configKeyMap];
    [entity setNetworkRequestType:CHSNetworkRequsetType];
    
    [CHSNetworkManager requestPOSTWithEntity:entity responseModel:nil successBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            if ([meta.data.data isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dataDic = (NSDictionary *)meta.data.data;
                NSString *timeStr = [dataDic objectForKey:@"appTimestamp"];
                BOOL needUpdate = NO;
                if (self.currentTimeTamp) {
                    if (![timeStr isEqualToString:self.currentTimeTamp]) {
                        needUpdate = YES;
                    }
                }else{
                    needUpdate = YES;
                }
                if (needUpdate) {
                    [self getDefaultModuleSystemConfigSuccessBlock:successBlock failureBlock:failureBlock];
                    self.currentTimeTamp = timeStr;
                    [[NSUserDefaults standardUserDefaults] setValue:timeStr forKey:@"appTimestamp"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
                
            }
        }
        successBlock([NSDictionary dictionary]);
    } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        failureBlock(meta.data.error);
    }];
}



#pragma mark 获取全量系统参数(本地json+远端配置参数)
-(void)getAllConfigWithRemoteAndLocalSuccessBlock:(SystemConfigSuccessBlock)successBlock
                   failureBlock:(SystemConfigFailureBlock)failureBlock{
    
    NSMutableDictionary *configKeyMap = [NSMutableDictionary dictionary];
    
    DataEntity * entity = [DataEntity new];
    [entity setUrlString:@"com.chs.app.mobileserver.config.timestamp"];
    [entity setParameters:configKeyMap];
    [entity setNetworkRequestType:CHSNetworkRequsetType];
    
    [CHSNetworkManager requestPOSTWithEntity:entity responseModel:nil successBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            if ([meta.data.data isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dataDic = (NSDictionary *)meta.data.data;
                NSString *timeStr = [dataDic objectForKey:@"appTimestamp"];
                BOOL needUpdate = NO;
                if (self.currentTimeTamp) {
                    if (![timeStr isEqualToString:self.currentTimeTamp]) {
                        needUpdate = YES;
                    }
                }else{
                    needUpdate = YES;
                }
                if (needUpdate) {
                    [self getDefaultModuleWithJsonAndOriginSuccessBlock:successBlock failureBlock:failureBlock];
                    self.currentTimeTamp = timeStr;
                    [[NSUserDefaults standardUserDefaults] setValue:timeStr forKey:@"appTimestamp"];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                }
                
            }
        }
        successBlock([NSDictionary dictionary]);
    } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        failureBlock(meta.data.error);
    }];

}

#pragma mark 根据对应key获取配置
-(void)requestSystemConfigKeyArray:(NSMutableArray*)keyArray SuccessBlock:(SystemConfigListSuccessBlock)successBlock
                          failureBlock:(SystemConfigFailureBlock)failureBlock{

    NSMutableDictionary *configKeyMap = [NSMutableDictionary dictionaryWithObjectsAndKeys:keyArray,@"configKeyMap", nil];
    
    DataEntity * entity = [DataEntity new];
    [entity setUrlString:@"com.chs.app.mobileserver.config.key"];
    [entity setParameters:configKeyMap];
    [entity setNetworkRequestType:CHSNetworkRequsetType];
    
    [CHSNetworkManager requestPOSTWithEntity:entity responseModel:nil successBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            successBlock(meta.data.data);
        }
    } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            failureBlock(meta.data.error);
        }
    }];
}

#pragma mark 根据对应key获取公共配置（无需登录）
-(void)requestSystemPubKeyArray:(NSArray*)keyArray SuccessBlock:(SystemConfigListSuccessBlock)successBlock
failureBlock:(SystemConfigFailureBlock)failureBlock{
    
    NSMutableDictionary *configKeyMap = [NSMutableDictionary dictionaryWithObjectsAndKeys:keyArray,@"configKeyMap", nil];
    
    DataEntity * entity = [DataEntity new];
    [entity setUrlString:@"com.chs.app.mobileserver.config.pubkey"];
    [entity setParameters:configKeyMap];
    [entity setNetworkRequestType:CHSNetworkRequsetType];
    
    [CHSNetworkManager requestPOSTWithEntity:entity responseModel:nil successBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            successBlock(meta.data.data);
        }
    } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            failureBlock(meta.data.error);
        }
    }];
    
}

#pragma mark 根据模块获取对应配置
-(void)requestSystemConfigModule:(NSString *)module SuccessBlock:(SystemConfigSuccessBlock)successBlock
                    failureBlock:(SystemConfigFailureBlock)failureBlock{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:module,@"module",nil];
    
    DataEntity * entity = [DataEntity new];
    [entity setUrlString:@"com.chs.app.mobileserver.config.module.key"];
    [entity setParameters:dict];
    [entity setNetworkRequestType:CHSNetworkRequsetType];
    __weak __typeof(&*self)weakSelf = self;
    [CHSNetworkManager requestPOSTWithEntity:entity responseModel:nil successBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            NSArray *configArray = (NSArray *)meta.data.data;
            NSDictionary *configDict = [NSDictionary dictionary];
            NSMutableDictionary *callBackDic = [NSMutableDictionary dictionary];
            for (int i = 0; i<configArray.count; i++) {
                configDict = [configArray objectAtIndex:i];
                id configValue = [configDict objectForKey:@"configValue"];
                NSString *configValueString;
                if ([configValue isKindOfClass:[NSDictionary class]] || [configValue isKindOfClass:[NSArray class]]) {
                    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:configValue options:0 error:nil];
                    configValueString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                }else{
                    configValueString = [NSString stringWithFormat:@"%@",[configDict objectForKey:@"configValue"]];
                }
                configValueString = [CAISystemConfigManager isEmpty:configValueString] ? @"" : configValueString;
                [callBackDic setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
                [weakSelf.systemDefaultConfigMap setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
            }
            [weakSelf writeConfigInfo:self.systemDefaultConfigMap];
            successBlock([callBackDic copy]);
        }
    } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            failureBlock(meta.data.error);
        }
    }];
}

#pragma mark 根据模块获取对应配置(无返回值)
-(void)requestSystemConfigModule:(NSString *)module{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:module,@"module",nil];
    
    DataEntity * entity = [DataEntity new];
    [entity setUrlString:@"com.chs.app.mobileserver.config.module.key"];
    [entity setParameters:dict];
    [entity setNetworkRequestType:CHSNetworkRequsetType];
    __weak __typeof(&*self)weakSelf = self;
    [CHSNetworkManager requestPOSTWithEntity:entity responseModel:nil successBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        if (meta) {
            NSArray *configArray = (NSArray *)meta.data.data;
            NSDictionary *configDict = [NSDictionary dictionary];
            for (int i = 0; i<configArray.count; i++) {
                configDict = [configArray objectAtIndex:i];
                id configValue = [configDict objectForKey:@"configValue"];
                NSString *configValueString;
                if ([configValue isKindOfClass:[NSDictionary class]] || [configValue isKindOfClass:[NSArray class]]) {
                    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:configValue options:0 error:nil];
                    configValueString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                }else{
                    configValueString = [NSString stringWithFormat:@"%@",[configDict objectForKey:@"configValue"]];
                }
                configValueString = [CAISystemConfigManager isEmpty:configValueString] ? @"" : configValueString;
                [weakSelf.systemDefaultConfigMap setValue:configValueString forKey:[configDict objectForKey:@"configKey"]];
            }
            [weakSelf writeConfigInfo:self.systemDefaultConfigMap];
        }
    } failureBlock:^(CHSNetworkMetaModel * _Nonnull meta) {
        
    }];
}

#pragma mark 获取默认模块远端配置（远端数据）
-(void)getDefaultModuleSystemConfigSuccessBlock:(SystemConfigSuccessBlock)successBlock
                                   failureBlock:(SystemConfigFailureBlock)failureBlock{
    [self requestSystemConfigModule:@"default" SuccessBlock:^(NSDictionary * _Nonnull systemConfigDic) {
        if (successBlock) {
            successBlock(systemConfigDic);
        }
    } failureBlock:^(NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}
#pragma mark 获取默认模块远端配置（远端+本地json文件）
-(void)getDefaultModuleWithJsonAndOriginSuccessBlock:(SystemConfigSuccessBlock)successBlock
                                        failureBlock:(SystemConfigFailureBlock)failureBlock{
    [self requestSystemConfigModule:@"default" SuccessBlock:^(NSDictionary * _Nonnull systemConfigDic) {
        if (successBlock) {
            successBlock(self.systemDefaultConfigMap);
        }
    } failureBlock:^(NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}


#pragma mark 获取本地单个系统参数
-(NSString *)getLocalConfigByKey:(NSString *)key{
    if ([CAISystemConfigManager isEmpty:key]) {
        return @"";
    }
    if (!self.systemDefaultConfigMap.allKeys.count) {
        return @"";
    }
    if (![self.systemDefaultConfigMap.allKeys containsObject:key]) {
        return @"";
    }
    
    id value;
    value = [self.systemDefaultConfigMap objectForKey:key];
    if (!value) {
        return @"";
    }
    NSString *vauleString;
    if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:value options:0 error:nil];
        vauleString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }else{
        vauleString = [NSString stringWithFormat:@"%@",value];
    }
    
    if ([CAISystemConfigManager isEmpty:vauleString]) {
        return @"";
    }else{
        return vauleString;
    }
}

#pragma mark 获取本地系统参数集合
-(NSDictionary *)getLocalConfigByKeys:(NSMutableArray *)keyMutableArray{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if (!keyMutableArray.count) {
        return [dict copy];
    }
    
    for (int i = 0; i < keyMutableArray.count; i++) {
        NSString *keyString = [keyMutableArray objectAtIndex:i];
        if (keyString.length <= 0) {
            break;
        }
        NSString * value = [self getLocalConfigByKey:keyString];
        [dict setValue:value forKey:keyString];
    }
    return [dict copy];
}

#pragma mark 读取本地json文件
- (NSDictionary *)caiReadLocalFileWithName:(NSString *)name
{
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    if (!path) {
        return [NSDictionary dictionary];
    }
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    
    return [NSJSONSerialization JSONObjectWithData:data
                                           options:kNilOptions
                                             error:nil];
}

#pragma mark 是否为第一次启动
-(BOOL )isFirstLauch{
    //获取当前版本号
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentAppVersion = infoDic[@"CFBundleVersion"];
    //获取上次启动应用保存的appVersion
    NSString *version = [[NSUserDefaults standardUserDefaults] objectForKey:AppVersion];
    //版本升级或首次登录
    if (version == nil || ![version isEqualToString:currentAppVersion]) {
        [[NSUserDefaults standardUserDefaults] setObject:currentAppVersion forKey:AppVersion];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return YES;
    }else{
        return NO;
    }
}

+ (BOOL)isEmpty:(NSString *)str{
    if (![str isKindOfClass:[NSString class]]) {
        return YES;
    }
    NSString * newSelf = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (nil == str
        || str.length ==0
        || [str isEqualToString:@""]
        || [str isEqualToString:@"<null>"]
        || [str isEqualToString:@"(null)"]
        || [str isEqualToString:@"null"]
        || newSelf.length ==0
        || [newSelf isEqualToString:@""]
        || [newSelf isEqualToString:@"<null>"]
        || [newSelf isEqualToString:@"(null)"]
        || [newSelf isEqualToString:@"null"]
        || [self isKindOfClass:[NSNull class]] ){
         return YES;
    }else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        return [trimedString isEqualToString: @""];
    }
    return NO;
}

- (void)writeConfigInfo:(NSDictionary *)dict {
    NSString * dictFilePath = [CAISystemConfigManager localDictionaryPath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL existed = [fileManager fileExistsAtPath:dictFilePath isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) ) {
        self.fileReCreateTimes ++;
        NSError * error = nil;
        [fileManager createDirectoryAtPath:dictFilePath withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            if (self.fileReCreateTimes >= 3) {
                return;
            }
            [self writeConfigInfo:dict];
            return;
        }
    }
    NSString *fileName = [CAISystemConfigManager configFileName];
    NSString * filePath = [dictFilePath stringByAppendingPathComponent:fileName];
    NSDictionary * congfigDict = @{@"info":(!dict ? @{} : dict)};
    [congfigDict writeToFile:filePath atomically:YES];
}

- (NSDictionary *)readConfigInfo {
    NSMutableDictionary *config = [@{} mutableCopy];
    NSString * filePath = [[CAISystemConfigManager localDictionaryPath] stringByAppendingPathComponent:[CAISystemConfigManager configFileName]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:filePath]) {
        [self writeConfigInfo:@{}];
        return config;
    }
    NSDictionary * localDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    if (localDict.allKeys.count && [localDict.allKeys containsObject:@"info"]) {
        if ([localDict objectForKey:@"info"] && [[localDict objectForKey:@"info"] isKindOfClass:[NSDictionary class]]) {
            [config removeAllObjects];
            [config addEntriesFromDictionary:[localDict objectForKey:@"info"]];
        }
    }
    return config;
}

+ (NSString *)localDictionaryPath {
    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString * dictFilePath = [docsdir stringByAppendingPathComponent:@"caiSystemConfig"];
    return dictFilePath;;
}

+ (NSString *)configFileName {
    return @"cai_system_config.plist";
}
@end
